<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>聊天室</title>
</head>
<body>
<h2>聊天室</h2>
<div id="content"></div><br>
<input id="msg" type="text">
<input type="button" id="btn" value="发送">
<script>
    //初始化websocket对象
    const ws = new WebSocket('ws://localhost:8080/chat');
    //接收服务端消息
    ws.onmessage = function(event){
        document.getElementById('content').innerHTML += "<div>" + event.data + "</div>";
    }

    //绑定事件发送消息
    document.getElementById("btn").addEventListener("click", function(){
        let msg = document.getElementById("msg").value;
        ws.send(msg);
    })

</script>
</body>
</html>